<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="线索名称">
          <a-input
            placeholder="请输入线索名称"
            v-decorator="['clueName', { rules: [{ required: true, message: '请输入线索名称!' }] }]"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人">
          <a-input
            placeholder="请输入联系人"
            v-decorator="['relaName', { rules: [{ required: false, message: '请输入联系人!' }] }]"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="手机号">
          <a-input placeholder="请输入手机号" type="number" v-decorator="['mobile',{
            rules: [
              { required: false, message: '请输入手机号码!' },
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
              ]},]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="电话(座机)">
          <a-input placeholder="请输入电话(座机)" v-decorator="['telephone', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="微信">
          <a-input placeholder="请输入微信" v-decorator="['wx', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="邮箱">
          <a-input placeholder="请输入邮箱" v-decorator="['email', { 
            rules: [{
             type: 'email', message: '请输入邮箱!',
            }]
          }]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="区域">
          <j-area-linkage  v-decorator="['province', {}]" type="cascader"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
          <a-input placeholder="请输入地址" v-decorator="['address', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="来源">
          <j-dict-select-tag v-decorator="['origin', { rules: [
              { required: true, message: '请选择来源!' },
              ] }]"  title="来源" :triggerChange=true
           dictCode="clueOrigin" placeholder="请选择来源" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
          <a-textarea aria-placeholder="请输入备注" v-decorator="['remark', {}]"></a-textarea>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import JAreaLinkage from '@comp/jeecg/JAreaLinkage'

export default {
  name: 'ClueModal',
  components: {
    JDictSelectTag,
    JAreaLinkage,
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {},
      url: {
        add: '/crm/clue/add',
        edit: '/crm/clue/edit',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'relaName',
            'clueName',
            'telephone',
            'mobile',
            'email',
            'wx',
            'province',
            'address',
            'origin',
            'remark'
          )
        )
       })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          if((formData.mobile==undefined||formData.mobile.length==0)
            &&(formData.telephone==undefined||formData.telephone.length==0)){
             that.$message.warning("手机或座机必须填写一项")
             return;
          }
          that.confirmLoading = true
          console.log(formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style lang="less" scoped>
</style>